﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Media;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Speed_of_live_and_deed
{
    class Game
    {
        /// <summary>
        /// 各种常量
        /// </summary>
        private const int GameWide = 1000;
        private const int GameHight = 700;
        /// <summary>
        /// 游戏自带时钟
        /// </summary>
        private System.Timers.Timer Timer { get; set; }
        //定义委托
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gameChanged;
        private Thief _thief;
        private Police _police;
        private String UserInput { get; set; }
        SoundPlayer soundPlayer = new SoundPlayer(Properties.Resources.backmusic); //游戏背景音乐
        private static DateTime StartTime { get; set; }
        private static DateTime Time { get; set; }
        public Game()
        {
            // 初始化_thief
            _thief = new Thief(new Rectangle(30, 420, 50, 60));
            //初始化_police
            _police = new Police(new Rectangle(216 - 6, 513 - 3, 50, 60));
            // _Words = new Words();
            UserInput = "";
            //判断游戏结束
            Touch();
            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
            Timer.Enabled = false;
        }

        /// <summary>
        ///         /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟处理程序
            _thief.Move();
            gameChanged();
        }

        /// <summary>
        /// 绘制游戏场景
        /// </summary>
        /// <param name="g"></param>
        /// <param name="size"></param>
        public void Draw(Graphics g, Size size)
        {
            //定义空白的图
            Image img = new Bitmap(GameWide, GameHight);
            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWide, GameHight));
            _g.DrawImage(Properties.Resources.background, rec);
            //画出thief类
            _thief.Draw(_g);
            //draw the words
            Rectangle recWord = new Rectangle(100, 600, 800, 50);
            _g.FillRectangle(new SolidBrush(Color.White), recWord);
            string st = Properties.Resources.Words;
            _g.DrawString(st, new Font("宋体", 24), new SolidBrush(Color.Green), recWord);
            //画出police类
            _police.Draw(_g);
            //判断游戏是否结束并画出图像
            if (_police.Rectangle.IntersectsWith(_thief.Rectangle))
            {
                _g.DrawImage(Properties.Resources.win, rec);
                _g.DrawString("游戏结束", new Font("微软雅黑", 50, FontStyle.Bold), Brushes.Blue, new PointF(600, 325));
                Touch();
                //显示所用时间
                Time = System.DateTime.Now;
                var a = Time - StartTime;
                string timeSpan = a.TotalSeconds.ToString();
                _g.DrawString("所用时间：", new Font("微软雅黑", 50, FontStyle.Bold), Brushes.Blue, new PointF(550, 500));
                _g.DrawString(timeSpan, new Font("微软雅黑", 50, FontStyle.Bold), Brushes.Blue, new PointF(850, 500));
            }
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));

        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="Key"></param>
        public void KeyDown(string key)
        {
            if (_police.Rectangle.IntersectsWith(_thief.Rectangle))
            {

            }
            else
            {
                _police.KeyDown(key);
            }

        }
        /// <summary>
        /// 选择困难难度
        /// </summary>
        public void btnDiffculty_Click()
        {
            _police.btnDiffculty_Click();
        }
        /// <summary>
        /// 选择一般难度
        /// </summary>
        public void btnOrdinary_Click()
        {
            _police.btnOrdinary_Click();
        }
        /// <summary>
        /// 选择简单难度
        /// </summary>
        public void btnEasy_Click()
        {
            _police.btnEasy_Click();
        }
        /// <summary>
        /// d
        /// </summary>
        public void Start()
        {
            Timer.Enabled = true;
            StartTime = System.DateTime.Now;
        }
        /// <summary>
        /// 停止游戏
        /// </summary>
        private void Touch()
        {
            if (_police.Rectangle.IntersectsWith(_thief.Rectangle))
            {
                Timer.Close();
                Timer.Stop();
                soundPlayer.Stop();
            }
        }
    }
}
